﻿#region

using System.Collections.Generic;
using System.Linq;
using Lab.DataContext.DataContext;
using Lab.Server.Domain;

#endregion

namespace Lab.Server.Infrastructure {
    public class EmployerRepository : Repository<Employer, employer> {
        public EmployerRepository(IDataContext dataContext) : base(dataContext) {}

        protected override employer TranslateToDataObject(Employer domainObject) {
            var employer = new employer {
                                            address_txt = domainObject.Address,
                                            employer_nm = domainObject.Name,
                                            telephone_nbr = domainObject.TelephoneNumber,
                                        };

            foreach (var kindOfActivity in domainObject.KindOfActivities) {
                employer.kind_of_activity_for_employers
                        .Add(new KindOfActivityRepository(DataContext).GetKindOfActivityForEmployer(kindOfActivity));
            }
            return employer;
        }

        public employer GetEmployerDataObject(Employer employer) {
            return DataContext.GetEmployers().Single(_ => _.employer_id == employer.Id);
        }

        public Employer ToDomainObject(employer employer) {
            var domainObject = new Employer {
                                                Address = employer.address_txt,
                                                Id = employer.employer_id,
                                                Name = employer.employer_nm,
                                                TelephoneNumber = employer.telephone_nbr,
                                            };

            var kindOfActivity = employer.kind_of_activity_for_employers
                                         .Select(kindOfActivityForEmployer =>
                                                 new KindOfActivityRepository(DataContext)
                                                     .TranslateToDomain(kindOfActivityForEmployer.kind_of_activity))
                                         .ToList();

            domainObject.KindOfActivities = kindOfActivity;

            return domainObject;
        }

        public List<Employer> GetEmployers() {
            return DataContext.GetEmployers().ConvertAll(ToDomainObject);
        }
    }
}